import Vue from 'vue'
import  * as apis from '../../services/apis'
import * as types from '../mutation-types'
import http from '../../http'

const state = {
  index: {},
  indexProduct: []
}

const getters = {
  index: state => state.index,
  indexProduct: state => state.indexProduct
}

const actions = {
  getIndex({
    commit
  }) {
     Vue.http.post(apis.GET_INDEX).then((res) => {
      commit(types.GET_INDEX, res.body)
    })
  },
  // 得到首页商品列表
  getIndexProduct({
    commit
  }, payload) {
    let url = apis.GET_INDEX_PRODUCT + '/' + payload.curPageNO
    Vue.http.post(url).then((res) => {
      commit(types.GETM2_PRO, res.body.result)
    })
  }
}

const mutations = {
  [types.GET_INDEX](state, payload) {
    state.index = payload.result
  },
  [types.GETM2_PRO](state, payload) {
    for (let item of payload.resultList) {
      state.indexProduct.push(item)
    }
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
